<cfcomponent output="false">
	
	<!--- --->
	
	<cffunction name="getValue" access="public" output="false" returntype="string">
		<cfargument name="name" required="true" />
		
		<cfset var value = "" />
		
		<cfif structKeyExists(form,arguments.name)>
			<cfset value = form[arguments.name] />
		</cfif>
		
		<cfif len(value) eq 0 and structKeyExists(url,arguments.name)>
			<cfset value = url[arguments.name] />
		</cfif>
		
		<cfreturn value />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="form" access="public" output="false" returntype="string">
		<cfargument name="cfc" required="true" />
		<cfargument name="method" required="true" />

		<cfreturn "/cfcs/action.cfm?cfc=#arguments.cfc#&method=#arguments.method#">
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getURL" access="public" output="false" returntype="string">
		<cfargument name="withArgs" required="false" default="false" />
		
		<cfset var result = cgi.SCRIPT_NAME />
		<cfif arguments.withArgs>
			<cfset result = result & '?' & cgi.query_string />
		</cfif>
		
		<cfreturn result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getReferrerURL" access="public" output="false" returntype="string">
		
		<cfset var local = {} />
		<cfset local.result = cgi.http_referer />
		<cfset local.offset = 7 />
		
		<cfif findNoCase(result,"https:")>
			<cfset local.offset = 8 />
		</cfif>
		
		<cfset local.offset = local.offset + len(cgi.http_host) />
		
		<cfset local.result = right(local.result,len(local.result) - local.offset) />
		
		<cfreturn local.result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="select" access="public" output="false" returntype="string">
		<cfargument name="name" required="true" />
		<cfargument name="query" required="true" type="query" />
		<cfargument name="value" required="false" default="" />
		<cfargument name="blank" required="false" default="false" />
		<cfargument name="option" required="false" default=" - " />
		
		<cfset var local = {} />
		
		<cfsavecontent variable="local.result">
			<cfoutput>
				<select name="#arguments.name#">
					<cfif arguments.blank>
						<option value="">#arguments.option#</option>
					</cfif>
					<cfloop query="arguments.query">
						<option value="#arguments.query.id#" <cfif arguments.value eq arguments.query.id>selected</cfif>>#arguments.query.name#</option>
					</cfloop>
				</select>
			</cfoutput>
		</cfsavecontent>

		<cfreturn local.result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="img" access="public" output="false" returntype="string">
		<cfargument name="path" required="true" />
		<cfargument name="id" required="false" default="" />
		<cfargument name="style" required="false" default="" />
		
		<cfset var local = {} />
		
		<cfsavecontent variable="local.result">
			<cfoutput>
				<img src="/assets/media/#arguments.path#" <cfif arguments.id neq "">id="#arguments.id#"</cfif> <cfif arguments.style neq ''>style="#arguments.style#"</cfif>/>
			</cfoutput>
		</cfsavecontent>

		<cfreturn local.result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="encrypt" access="public" output="false" returntype="string">
		<cfargument name="string" required="true" />
		
		<cfreturn encrypt(arguments.string,application.vars.encryptKey,'desede','base64') />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="decrypt" access="public" output="false" returntype="string">
		<cfargument name="string" required="true" />
		
		<cfreturn decrypt(arguments.string,application.vars.encryptKey,'desede','base64') />
		
	</cffunction>
	
	<!--- --->
		
	<cffunction name="addMessage" access="public" output="true" returntype="void">
		
		<cfset application.s.alertService.addMessage(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getMessages" access="public" output="true" returntype="array">

		<cfreturn application.s.alertService.getMessages() />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="dbSave" access="public" output="true" returntype="string">
		<cfargument name="table" required="true" />
		<cfargument name="id" required="false" default="" />
	
		<cfset var local = {} />
		
		<cfif arguments.id eq "">
			<cfset local.id = _dbInsert(argumentCollection=arguments) />
		<cfelse>
			<cfset local.id = _dbUpdate(argumentCollection=arguments) />
		</cfif>
		
		<cfreturn local.id />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="_dbUpdate" access="private" output="true" returntype="string">

		<cfset var local = {} />
		
		<cfset local.id = arguments.id />
		<cfset local.table = application.vars.db[arguments.table] />
		<cfset local.script = "" />
		
		<cfset structDelete(arguments,"table") />
		<cfset structDelete(arguments,"id") />
		<cfset structDelete(arguments,"cfc") />
		<cfset structDelete(arguments,"callMethod") />
		
		<cfloop collection="#arguments#" item="local.i">
			<cfif arguments[local.i] eq "">
				<cfset arguments[local.i] = "null" />
				<cfset local.script = listAppend(local.script,local.i & " = " & arguments[local.i]) />
			<cfelse>
				<cfset local.i = replace(local.i,"'","\'","ALL") />
				<cfset local.script = listAppend(local.script,local.i & " = '" & arguments[local.i] & "'") />
			</cfif>
		</cfloop>
		
		<cfif local.table.timestamp>
			<cfset local.script = local.script & ", modified = now()" />
		</cfif>
		
		<cftransaction>
			<cfquery name="local.dbupdate" datasource="fundrdb">
				update #local.table.name#
				set #local.script#
				where id = '#local.id#'
			</cfquery>
		</cftransaction>

		<cfreturn local.id />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="_dbInsert" access="private" output="true" returntype="string">

		<cfset var local = {} />
		
		<!--- capture the values which will be removed from arguments --->
		<cfset local.table = application.vars.db[arguments.table] />
		
		<cfset local.columns = "" />
		<cfset local.values = "" />
		
		<!--- remove from arguments --->
		<cfset structDelete(arguments,"table") />
		<cfset structDelete(arguments,"id") />
		<cfset structDelete(arguments,"cfc") />
		<cfset structDelete(arguments,"callMethod") />
		
		<cfloop collection="#arguments#" item="local.i">
			<cfset replace(local.i,"'","''","ALL") />
			<cfif arguments[local.i] eq "">
				<cfset arguments[local.i] = "null" />
				<cfset local.values = listAppend(local.values,arguments[local.i]) />
			<cfelse>
				<cfset local.i = replace(local.i,"'","\'","ALL") />
				<cfset local.values = listAppend(local.values,"'" & arguments[local.i] & "'") />
			</cfif>
			<cfset local.columns = listAppend(local.columns,local.i) />
		</cfloop>
		
		<cfif local.table.active>
			<cfset local.columns = listAppend(local.columns,'active') />
			<cfset local.values = listAppend(local.values,'1') />
		</cfif>
		
		<cfif local.table.timestamp>
			<cfset local.columns = local.columns & ",created,modified" />
			<cfset local.values = local.values & ",now(),now()" />
		</cfif>
		
		<cfset local.id = newID() />
		
		<cfquery name="local.dbinsert" datasource="fundrdb">
			insert into #local.table.name#(id,#local.columns#)
			values(#local.id#,#local.values#)
		</cfquery>
		
		<cfreturn local.id />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="newID" access="public" output="false" returntype="string">
		<cfargument name="now()">

		<cfset var local = {} />
		
		<cfquery name="local.newID" datasource="fundrdb">
			select cast(uuid_short() as char) as id;
		</cfquery>
		
		<cfreturn local.newID.id />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="checkbox" access="public" output="true" returntype="string">
		<cfargument name="id" required="true" />
		<cfargument name="label" required="true" />
		<cfargument name="value" required="false" default="1" />
		<cfargument name="checked" required="false" default="false">
		<cfargument name="name" required="false" default="#arguments.id#" />

		<cfset var local = {} />
		
		<cfsavecontent variable="local.result">
			<cfoutput>
				<div>
					<input id="#arguments.name#" type="checkbox" name="#arguments.name#" value="#arguments.value#" <cfif arguments.checked>checked</cfif> />
					<label for="#arguments.name#">#label#</label>
				</div>
			</cfoutput>
		</cfsavecontent>
		
		<cfreturn local.result />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="formatDate" access="public" output="true" returntype="string">
		<cfargument name="date" required="true" />
		
		<cfreturn dateFormat(arguments.date,"yyyy-mm-dd") & " 00:00:00" />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="getVar" access="public" output="false" returntype="string">
		<cfargument name="name" required="true" />
		
		<cfset var local = {} />
		<cfset local.locale = "www" />
		<cfset local.app = "pong" />

		<cfif cgi.http_referer contains "pong.cfm" or cgi.cf_template_path contains 'pong.cfm'>
			<cfset local.app = "pong" />
		</cfif>
		
		<cfif cgi.http_referer contains "home.cfm" or cgi.cf_template_path contains 'home.cfm'>
			<cfset local.app = "fundr">
		</cfif>
		
		<cfif cgi.http_host contains 'localhost'>
			<cfset local.locale = "local" />
		</cfif>
		<cfif cgi.http_host contains 'www'>
			<cfset local.locale = 'web' />
		</cfif>
		
		<cfset local.key = local.locale & local.app />
		
		<cfreturn application.vars[local.key][arguments.name] />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="queryID" access="public" output="true" returntype="string">
		<cfargument name="name" required="true" />
		
		<cfset var local = {} />
		
		<cfsavecontent variable="local.result">
			<cfqueryparam value="#arguments.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="17" />
		</cfsavecontent>
		
		<cfreturn local.result />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="log" access="public" output="true" returntype="string">
		<cfargument name="item" required="true" />
		
		<cfset session['log_#now()##createuuid()#'] = arguments.item />

	</cffunction>
	
	<!--- --->
	
	<cffunction name="isEven" access="public" output="true" returntype="string">
		<cfargument name="number" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.result = true />
		
		<cfif not isNumeric(arguments.number) or arguments.number eq "">
			<cfset local.result = false />
		<cfelse>
			<cfif arguments.number mod 2 eq 1>
				<cfset local.result = false />
			</cfif>
		</cfif>
		
		<cfreturn local.result />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="isOdd" access="public" output="true" returntype="string">
		<cfargument name="number" required="true" />
		
		<cfreturn not isEven(number=arguments.number) />
		
	</cffunction>
	
	<!--- --->
	
</cfcomponent>